var app = getApp()
var verify = require('../../../utils/verify-token.js');
var base64 = require("../../../images/base64");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		ListData: []

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		//  console.log(options)
		this.setData({
			sh: app.globalData.screenHeight,
			sw: app.globalData.screenWidth,
			deviceId: options.qrId,
			icon60: base64.icon60,
			replenishallModal: false,
			replenishModal: false,
			date: '请点击选择有效期',
			count: 0
		});
		let that = this;
		that.getDevInfo();
		that.getList();
	},
	getDevInfo: function() {
		let that = this;
		let deviceId = that.data.deviceId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/info',
			method: 'get',
			header: {
				'AUTHORIZATION': app.globalData.token
			},
			success: function(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					let devInfo = res.data.data;
					that.setData({
						devInfo: devInfo
					})
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	getList: function() {
		let thm = this;
		let deviceId = thm.data.deviceId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/goods_aisle/list',
			method: 'get',
			header: {
				'AUTHORIZATION': app.globalData.token
			},
			success: function(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					let listdata = res.data.data;
					thm.setData({
						ListData: listdata
					})
					// console.log(thm.data.ListData);
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	replenishAll() {
		this.setData({
			replenishallModal: true
		})
	},
	replenishOne(res) {
		console.log(res);
		let oneDetail = res.currentTarget.dataset.itm;
		this.setData({
			replenishModal: true,
			oneDetail: oneDetail
		})
	},
	cancel() {
		this.setData({
			replenishallModal: false,
			replenishModal: false
		})
	},
	replenish() {
		let that = this;
		let deviceId = that.data.deviceId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/replenish/all',
			method: 'post',
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				deviceId: deviceId
			},
			success: function(res) {
				that.setData({
					replenishallModal: false
				})
				if (res.data.code == 60006) {
					verify.clearAll();
				} else if (res.data.code == 200) {

					wx.showToast({
						title: '指令下发成功',
						icon: 'success',
						duration: 2000
					})
					that.getList();

				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	bindDateChange: function(e) {
		// console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			date: e.detail.value
		})
	},
	countInput: function(e) {
		this.setData({
			count: e.detail.value
		})
	},
	//单个补货
	replenishone() {
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.oneDetail.deviceGoodsAisleId;
		let count = that.data.count;
		let date = that.data.date == '请点击选择有效期' ? '' : that.data.date;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/replenish',
			method: 'post',
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				deviceId: deviceId,
				deviceGoodsAisleId: deviceGoodsAisleId,
				count: count,
				effectiveTime: date
			},
			success: function(res) {
				that.setData({
					replenishModal: false
				})
				if (res.data.code == 60006) {
					verify.clearAll();
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '指令下发成功',
						icon: 'success',
						duration: 2000
					})
					that.getList();

				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//单个补满
	replenishMax() {
		let that = this;
		let count = that.data.oneDetail.volume;
		wx.showModal({
			title: "提示",
			content: "确定补满吗？",
			success(res) {
				if (res.confirm) {
					that.setData({
						count: count,
						date: ''
					})
					that.replenishone();
				}
			}
		})


	},
	//全部开门
	openAll(){
		let that = this;
		wx.showModal({
			title: "提示",
			content: "确定要一键开门吗？",
			success(res) {
				if (res.confirm) {
					that.openDoor('All');
				}
			}
		})
	},
	//单个开门
	open() {
		let that = this;
		wx.showModal({
			title: "提示",
			content: "确定要开门吗？",
			success(res) {
				if (res.confirm) {
					that.openDoor('One');
				}
			}
		})
	},
	openDoor(flag) {
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = flag=='One'?that.data.oneDetail.deviceGoodsAisleId:'all';
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/open/door',
			method: 'post',
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				deviceId: deviceId,
				deviceGoodsAisleId: deviceGoodsAisleId
			},
			success: function(res) {
				that.setData({
					replenishModal: false
				})
				if (res.data.code == 60006) {
					verify.clearAll();
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '指令下发成功',
						icon: 'success',
						duration: 2000
					})

				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {


	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
